package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.entity.shop.TotalOrderListData;
import com.mvp.shq.washer.entity.shop.result.ProductResult;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.StringUtils;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by westing on 2017/10/11.
 * 多商品订单列表
 */

public class OrderAllListAdapter extends RecyclerView.Adapter<OrderAllListAdapter.ViewHolder> {
    private Context context;
    private List<TotalOrderListData> productList;

    public OrderAllListAdapter(Context mContext, List<TotalOrderListData> productList) {
        context = mContext;
        this.productList = productList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_orderall, parent, false);
        return new ViewHolder(view);
    }

    double m1 = 0, integra1 = 0;
    String money2 = "";

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.recyclerview.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, 10, ContextCompat.getColor(context, R.color.white));
        holder.recyclerview.addItemDecoration(divider);
        OrderListAdapter listadapter = new OrderListAdapter(context, productList.get(position).getProducts());
        holder.recyclerview.setAdapter(listadapter);

        holder.tvNum.setText(""+productList.get(position).getProducts().size());
        holder.tvListnum.setText("共" + productList.get(position).getProducts().size() + "件商品");
        holder.tvTime.setText("预计明天能送达");//预计运送时间
//        holder.tvMessage.setText("");//买家留言

        for (TotalOrderListData productResult : productList) {
            for (int i = 0,len= productResult.getProducts().size(); i <len; i++) {
                Product product = productResult.getProducts().get(i);
                if (product.getPayMode().equals(DataDictionary.PAY_MODE_CASH_AND_INTEGRAL)) {
                    m1 += product.getCashPrice()*product.getByNum();
                    money2 = StringUtils.BigDecimalToStr(BigDecimal.valueOf(m1));
                    integra1 += (int) product.getIntegralPrice()*product.getByNum();
                } else if (product.getPayMode().equals(DataDictionary.PAY_MODE_CASH)) {

                    m1 += product.getCashPrice()*product.getByNum();
                    money2 = StringUtils.BigDecimalToStr(BigDecimal.valueOf(m1));
                    integra1 = 0;
                    holder.tvAdd.setVisibility(View.GONE);
                    holder.llIntegral.setVisibility(View.GONE);
                } else {
                    holder.llMoney.setVisibility(View.GONE);
                    holder.tvAdd.setVisibility(View.GONE);
                    integra1 += (int) product.getIntegralPrice()*product.getByNum();
                }
            }
        }
        holder.tvMoney1.setText("" + (int) m1);
        holder.tvMoney2.setText("" + money2);
        holder.tvIntegral.setText("" + (int) integra1);
    }


    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


    @Override
    public int getItemCount() {
        return productList != null ? productList.size() : 0;
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.recyclerview)
        RecyclerView recyclerview;
        @BindView(R.id.tv_time)
        TextView tvTime;//预计运送时间
        @BindView(R.id.tv_message)
        EditText tvMessage;//买家留言
        @BindView(R.id.tv_listnum)
        TextView tvListnum;//共计商品数量
        @BindView(R.id.tv_money_1)
        TextView tvMoney1;//小计:元
        @BindView(R.id.tv_money2)
        TextView tvMoney2;//小计:角分
        @BindView(R.id.ll_money)
        LinearLayout llMoney;
        @BindView(R.id.tv_add)
        TextView tvAdd;
        @BindView(R.id.tv_integral)
        TextView tvIntegral;//小计:积分
        @BindView(R.id.ll_integral)
        LinearLayout llIntegral;
        @BindView(R.id.tv_num)
        TextView tvNum;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
